/* $Header: display_ui.c,v 2.2 01/10/02 10:26:16 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME: display_ui.c                                           */
/* ============                                                        */
/*                                                                     */
/* Author:                                                             */
/*                                                                     */
/* MODULE PURPOSE: Functions for datacomm access to the display.       */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include  <typedefs.h>
#include  <p_wkfile.h>
#include  <wkfile.h>
#include  <ramps.h>
#include  <zone_config.h>
#include  <zone_status.h>
#include  <status.h>
#include  <i_op.h>
#include  <display.h>
#include  <display_ui.h>
#include  <msg.h>


/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/


/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void RestoreDisplay (void)
{
   External_Display = FALSE;
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

static BIT8  StillRoom (INT8 j)
{
   return (j < (External_Image.next_index + DISPLAY_LINE_LENGTH));
}


/***********************************************************************/
/* FUNCTION:  SetDisplayLine                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  Puts characters in "line" into display.  If line is equal */
/*    or shorter than display length, the display normal & blink images*/
/*    are the same (ie: no blinking.).  If line is shorter than the    */
/*    display length, the display line is filled out with blanks.      */
/*                                                                     */
/*    If line is longer than display length, the remaining characters  */
/*    (up through line[2*display_length]) are placed in the blink      */
/*    image. If there are not enough characters in this second part of */
/*    line, the rest of the blink image will still contain the         */
/*    characters duplicated from the normal_image.                     */
/*                                                                     */
/*    The first time the module is called, it blanks the display       */
/*    as it switches to the External_Display mode.                     */
/*                                                                     */
/* RETURNED VALUE:  None.   The line_no is retricted to 1 through 4    */
/*      before this function called.  Any string (terminated by 00)    */
/*      is ok.                                                         */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

void SetDisplayLine (BIT8* line, U_INT8 line_no)
{
   INT8 i;
   INT8 j;

   External_Display = TRUE;

   j = ( (line_no - 1) * DISPLAY_LINE_LENGTH ) + 1;
   /* j = 1, 21, 41, or 61 for 20 char lines */

   External_Image.next_index = j;

   i = 0;

   while ( (line[i] != 0) && (StillRoom(j)) )
   {
      External_Image.normal_image[j]  = line[i];
      External_Image.blink_image[j++] = line[i++];
   }

   while ( StillRoom(j) )  /* Fill any remaining characters with blanks */
   {
      External_Image.normal_image[j]  = ' ';
      External_Image.blink_image[j++] = ' ';
   }

   j = External_Image.next_index;

   while ( (line[i] != 0) && (StillRoom(j)) ) /* Add blink image (if any) */
   {
      External_Image.blink_image[j++] = line[i++];
   }
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/


const BIT8 Blank_Line[] = " ";

void ClearDisplay (void)
{
   SetDisplayLine ((BIT8 *) Blank_Line, 1);
   SetDisplayLine ((BIT8 *) Blank_Line, 2);
   SetDisplayLine ((BIT8 *) Blank_Line, 3);
   SetDisplayLine ((BIT8 *) Blank_Line, 4);
}


/***********************************************************************/
/* FUNCTION:                                                           */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:                                                            */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/***********************************************************************/

INT16 GetDisplayLine (BIT8 *p_resp_parm, U_INT8 line_no)
{
   DISP_IMAGE* ptr;
   INT8        j;
   INT8        i;
   INT8        i2;

   if (External_Display)
   {
      ptr = &External_Image;
   }
   else
   {
      ptr = &Disp_Image;
   }

   j = ( (line_no - 1) * DISPLAY_LINE_LENGTH ) + 1;
   /* j = 1, 21, 41, or 61 for 20 char lines */

   i   = 0;
   i2  = DISPLAY_LINE_LENGTH;

   INTERRUPTS_OFF

   while (i < DISPLAY_LINE_LENGTH)
   {
      p_resp_parm[i++]  = ptr->normal_image[j];
      p_resp_parm[i2++] = ptr->blink_image[j++];
   }

   INTERRUPTS_ON

   return (2 * DISPLAY_LINE_LENGTH);
}

